package com.zxl.openapi.controller;


import com.zxl.openapi.api.RedisServiceApi;
import com.zxl.openapi.exception.RedisException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

import static com.zxl.openapi.exception.ExceptionDict.*;

@RestController
@RequestMapping("/cache")
@Api(description="缓存接口的controller")
public class CacheController {

    @Autowired
    private RedisServiceApi redisServiceApi;

    /**
     * 以字符串的形式保存
     * @param key
     * @param value
     * @param expire
     */
    @ApiOperation("添加字符串到缓存")
    @RequestMapping(value = "/save2cache", method = RequestMethod.GET)
    public boolean save2cache(@ApiParam(name = "key", required = true, defaultValue = "laowang") String key, @ApiParam(name = "value", required = true, defaultValue = "qianfengdiyijian") String value, @ApiParam(required = true, defaultValue = "10000") long expire) {
        //有效期是个数字,用户就可能会传递负数
        //有效期为负数的情况下我们应该如何处理,指的是你们如何处理,比如不管,报错,转成正数,认为不设置有效期等等都可以

        //我们无法控制用户,所以用户可能会出现参数不传递的情况
        checkKey(key);
        return redisServiceApi.set(key, value, expire);

    }

    @RequestMapping("/memebers/{key}")
    public Set<String> members(@PathVariable String key){

        checkKey(key);

        return redisServiceApi.getSet(key);
    }


    @GetMapping("/hgetall/{key}")
    public Map<Object,Object> hgetAll(@PathVariable String key){

        checkKey(key);

        return redisServiceApi.hGetAll(key);
    }

    @GetMapping("/get/{key}")
    public String get(@PathVariable String key){

        checkKey(key);

        return redisServiceApi.get(key);

    }

    private void checkKey(String key){

        if(key == null || StringUtils.isEmpty(key)){

            throw new RedisException(REDIS_KEY_IS_NULL,"redis的key不能为空");

        }

    }
    @RequestMapping("/hget/{key}/{field}")
    public String hget(@PathVariable String key, @PathVariable String field) {
        checkKey(key);
        checkKey(field);
        return redisServiceApi.hget(key, field);
    }


    @RequestMapping("/hincr/{key}/{field}")
    public long hdecr(@PathVariable String key, @PathVariable String field, @RequestParam(defaultValue = "0") long increment) {
        checkKey(key);

        checkKey(field);

        increment = increment == 0 ? -1 : increment;

        return redisServiceApi.hdecr(key, field, increment);
    }

    @RequestMapping("/keys/{partten}")
    public Set<String> keys(@PathVariable  String partten) {

        return redisServiceApi.keys(partten);
    }

    @PostMapping("/mput/{key}")
    public boolean mPut(@PathVariable String key, @RequestBody Map fieldValue) {
        checkKey(key);
        redisServiceApi.mPut(key, fieldValue);
        return true;
    }

}
